package Io;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Test6 {
    //文件字符流输入输出
    public static void main(String[] args) {
//        Test6.testFileRead("D:\\aa\\a\\tt1.txt");
//        Test6.testFileWrite("顾新宇","D:\\aa\\a\\tt2.txt");
        Test6.copyFile("D:\\aa\\a\\b\\tt1.txt","D:\\aa\\a\\b\\tt4.txt");
    }

    public static void testFileRead(String inpath) {
        try {
            FileReader fr = new FileReader(inpath);
            char[] c = new char[10];
            int len = 0;
            while ((len = fr.read(c)) != -1) {
                System.out.println(new String(c, 0, len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void testFileWrite(String str,String outpath) {
        try {
            FileWriter fw=new FileWriter(outpath);
            fw.write(str);
            fw.flush();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //用字符流复制文件
    public static void copyFile(String inpath,String outpath){
        try {
            FileReader fr = new FileReader(inpath);
            FileWriter fw=new FileWriter(outpath);
//            char [] c=new char[100];
//            int len=0;
//            while ((len=fr.read(c))!=-1){
//                fw.write(c,0,len);
//            }
//            fw.flush();
//            fw.close();
//            fr.close();
            int len = 0;
            while ((len = fr.read())!=-1){
                fw.write(len);
            }
            fw.flush();
            fw.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}